Puts the items of target in order. Numbers go smallest first when ascending is true and biggest first when it is false. Text goes in alphabetical order when ascending is true (Z to A when it is false), with one catch: every capital letter comes before every small letter. It changes target itself and makes no copy. A list of yes-or-no values, structs or references is refused when the project is checked.
list_sort(target, ascending)| Name | Type |
|---|---|
target | list |
ascending | bool |
Nothing.
Puts the items of target, a list, in order. For numbers, when ascending is true the smallest comes first, and when it is false the biggest comes first, which is the way a high-score table is usually shown. For text, true puts the A's first and false puts the Z's first (there is a catch about capital letters, below).
list_sort(scores, true); // 10, 20, 30
list_sort(scores, false); // 30, 20, 10
list_sort(names, true); // "Ada", "Bo", "Cy"list_sort rearranges the items that are already in target. It does not make a new list, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with list_copy first and sort the one you want sorted.
It sorts the items the list holds right now, the number list_length gives you. The empty places up to the capacity are not touched, and a list with no items or one item is left as it is.
A list of whole numbers or decimals is put in number order, and a list of text is put in alphabetical order. A list of yes-or-no values, of structs or of references is refused when the project is checked, before the game runs: GBML does not know what order those should go in, so it will not guess.
| The list holds | What happens |
|---|---|
whole numbers (list<int>) | sorted |
decimals (list<real>) | sorted |
text (list<string>) | sorted, in the order below |
| yes-or-no values, structs or references | refused when checked |
If you only want the order mixed up, and not put in any order, list_shuffle works on a list of anything.
Text is put in order one character at a time, by each character's code. The code is the number a character has in the table of letters ("A" is 65, "a" is 97, "0" is 48, a space is 32: see string_code_at). That gives these rules:
| Rule | Example |
|---|---|
| a space comes before a digit, a digit before a capital letter, a capital letter before a small letter | " x", "10", "Zed", "ada" come in that order |
| every capital letter comes before every small letter. A capital and a small letter are not treated as the same letter | "Zed" comes before "ada", and "Apple" before "apple" |
| digits are compared one at a time | "10" comes before "9", because 1 comes before 9 |
| a text that is the start of a longer one comes first | "ab" comes before "abc" |
| a character past the plain keyboard ones (a code above 127) comes after all of them | it comes last |
If you want names to come out the way a dictionary would put them, write them all with a capital first letter, or all small. With ascending set to false the whole order is turned around.
| You do this | What happens |
|---|---|
| sort a list that has equal numbers, or equal pieces of text, in it | they end up next to each other. Two equal values are the same value, so it does not matter which is which |
leave ascending out, or give a number instead of true or false | refused when checked |
| give it an array or a grid | refused when checked, and the message names the function that sorts that kind, like array_sort |
| give it a stack or a queue | refused when checked. A stack and a queue give things back in the order they went in, so sorting one would undo what it is for |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes a list as a parameter works on the caller's own list (see Lists). So list_sort inside a script sorts the caller's list.
Nothing is set aside: the sort is done in place, one item at a time. The time grows a little faster than the number of items, and comparing two pieces of text takes longer than comparing two numbers. A list of a few dozen items is nothing to worry about, but sorting a very long list every frame is something to avoid. No timing was measured.
/// @type {list<int>}
var scores = [30, 10, 20];
list_sort(scores, true); // 10, 20, 30
var lowest = scores[0]; // 10
list_sort(scores, false); // 30, 20, 10
var highest = scores[0]; // 30
/// @type {list<string>}
var names = ["pear", "Zed", "ada"];
list_sort(names, true); // "Zed", "ada", "pear": a capital letter comes before a small oneSorts the list_length(target) items the list holds - never its capacity - with a shell sort over the vector, in place. A shell sort done in place: it uses no extra memory, makes no copy, and never leaves the places that hold items. It is not stable, which cannot show for numbers or text (two equal values are the same value). The time grows a little faster than the number of items - about n times the square root of n in the worst case, usually much less - and comparing two texts takes longer than comparing two numbers (as many steps as the two texts have in common at the start). No timing was measured. Whole numbers, decimals and text build. Text is put in order one character at a time, by each character's code. A space comes before a digit, a digit before a capital letter and a capital letter before a small one, so "Zed" comes before "ada" (a capital and a small letter are NOT treated as the same letter), and a text that is the start of a longer one comes first ("ab" before "abc"). A character with a code above 127 comes after all the plain keyboard ones. There is no order for yes-or-no values, structs or references that GBML knows, so those are refused when the project is checked.